package blackbirdisoserver;

import java.sql.*;

/**
 * This class is used for actual interaction with the database like creating a
 * connection, executing queries, and nonQueries.
 * 
 */
public class ConnectionManager {

	/**
	 * The name of the database.
	 */
	public static final String DATABASE = "jpos_serverd_pos";

	/**
	 * The username used for connecting to the database.
	 */
	public static final String USER = "root";

	/**
	 * The password used for connecting to the database.
	 */
	public static final String PASSWORD = "hellothere";
	
	/**
	 * The port used for connecting to the database.
	 */
	public static final String PORT = "3306";

	/**
	 * The whole connection string for the database.
	 */
	public static final String CONNECTIONSTRING = "jdbc:mysql://localhost:"+PORT+"/"
			+ DATABASE + "?" + "user=" + USER + "&password=" + PASSWORD;

	private static Connection conn;

	/**
	 * Creates a connection to the database using the
	 * <code>ConnectionManager.CONNECTIONSTRING</code>.
	 */
	public static Connection createConnection() {
		try {
			Class.forName("org.gjt.mm.mysql.Driver").newInstance();
			conn = DriverManager.getConnection(CONNECTIONSTRING);
			conn.setAutoCommit(false);
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Executes the specified <code>query</code> and returns a
	 * <code>ResultSet</code>. It doesn't close the connection after it
	 * completes, so you have to close it yourself.
	 * 
	 * @param query
	 *            the query.
	 * @return the ResultSet resulting from the query.
	 */
	public static ResultSet executeQuery(String query) {
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			return rs;
		} catch (SQLException e) {
			e.printStackTrace();			
			return null;
		}
	}

	/**
	 * Takes an SQL <code>command</code>, this command can be a DDL command or a
	 * DML command, executes it and returns the number of rows affected. You
	 * have to commit and close the connection yourself.
	 * 
	 * @param command
	 *            a DDL or a DML command.
	 * @return the number of rows affected from executing the command. If the
	 *         statement failed, -1 is returned (because no rows are really
	 *         affected).
	 */
	public static int executeNonQuery(String command) {
		int rowsAffected = _executeNonQuery(command);
		return rowsAffected;
	}

	private static int _executeNonQuery(String command) {
		try {
			Statement stmt = conn.createStatement();
			int rowsAffected = stmt.executeUpdate(command);
			return rowsAffected;
		} catch (Exception e) {
			e.printStackTrace();			
			return -1;
		}
	}

	public static void close() {
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	public boolean commit() {
		try {
			conn.commit();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
